package vip.xiaonuo.biz.modular.banner.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.xiaonuo.auth.core.util.StpLoginUserUtil;
import vip.xiaonuo.biz.modular.banner.entity.BizBanner;
import vip.xiaonuo.biz.modular.banner.mapper.BizBannerMapper;
import vip.xiaonuo.biz.modular.banner.param.BizBannerAddParam;
import vip.xiaonuo.biz.modular.banner.param.BizBannerEditParam;
import vip.xiaonuo.biz.modular.banner.param.BizBannerIdParam;
import vip.xiaonuo.biz.modular.banner.param.BizBannerPageParam;
import vip.xiaonuo.biz.modular.banner.result.BizBannerListResult;
import vip.xiaonuo.biz.modular.banner.service.BizBannerService;
import vip.xiaonuo.common.enums.CommonSortOrderEnum;
import vip.xiaonuo.common.exception.CommonException;
import vip.xiaonuo.common.page.CommonPageRequest;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Banner服务实现类
 *
 * @author SNOWY_TEAM
 **/
@Service
public class BizBannerServiceImpl extends ServiceImpl<BizBannerMapper, BizBanner> implements BizBannerService {

    @Override
    public Page<BizBanner> page(BizBannerPageParam bizBannerPageParam) {
        QueryWrapper<BizBanner> queryWrapper = new QueryWrapper<>();

        // 查询条件
        if(ObjectUtil.isNotEmpty(bizBannerPageParam.getName())) {
            queryWrapper.lambda().like(BizBanner::getName, bizBannerPageParam.getName());
        }
        if(ObjectUtil.isNotEmpty(bizBannerPageParam.getStatus())) {
            queryWrapper.lambda().eq(BizBanner::getStatus, bizBannerPageParam.getStatus());
        }
        if(ObjectUtil.isNotEmpty(bizBannerPageParam.getSpotId())) {
            queryWrapper.lambda().eq(BizBanner::getSpotId, bizBannerPageParam.getSpotId());
        }

        // 排序 - 直接按sort字段升序排列
        queryWrapper.lambda().orderByAsc(BizBanner::getSort);

        return this.page(CommonPageRequest.defaultPage(), queryWrapper);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(BizBannerAddParam bizBannerAddParam) {
        BizBanner bizBanner = BeanUtil.toBean(bizBannerAddParam, BizBanner.class);
        bizBanner.setId(IdUtil.fastSimpleUUID());
        bizBanner.setCreateTime(new Date());
        bizBanner.setCreateUser(StpLoginUserUtil.getLoginUser().getId());
        this.save(bizBanner);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(BizBannerEditParam bizBannerEditParam) {
        BizBanner bizBanner = this.queryEntity(bizBannerEditParam.getId());
        BeanUtil.copyProperties(bizBannerEditParam, bizBanner);
        bizBanner.setUpdateTime(new Date());
        bizBanner.setUpdateUser(StpLoginUserUtil.getLoginUser().getId());
        this.updateById(bizBanner);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(BizBannerIdParam bizBannerIdParam) {
        this.removeById(bizBannerIdParam.getId());
    }

    @Override
    public BizBanner detail(BizBannerIdParam bizBannerIdParam) {
        return this.queryEntity(bizBannerIdParam.getId());
    }

    @Override
    public List<BizBannerListResult> getEnabledBannerList() {
        LambdaQueryWrapper<BizBanner> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BizBanner::getStatus, 1)  // 查询启用状态的banner
                .orderByAsc(BizBanner::getSort);  // 按sort字段升序排序

        List<BizBanner> bannerList = this.list(queryWrapper);

        List<BizBannerListResult> resultList = new ArrayList<>();
        if(CollectionUtil.isNotEmpty(bannerList)) {
            bannerList.forEach(banner -> {
                BizBannerListResult result = new BizBannerListResult();
                BeanUtil.copyProperties(banner, result);
                resultList.add(result);
            });
        }

        return resultList;
    }

    /**
     * 获取Banner实体
     *
     * @param id Banner ID
     * @return Banner实体
     */
    private BizBanner queryEntity(String id) {
        BizBanner bizBanner = this.getById(id);
        if(ObjectUtil.isEmpty(bizBanner)) {
            throw new CommonException("Banner不存在，id值为：" + id);
        }
        return bizBanner;
    }
}
